/*
  参数请求：import { request } from "../../request/index";
  导入解决报错的包：import regeneratorRuntime from '../../lib/runtime/runtime';
*/
import { request } from "../../request/index";
import regeneratorRuntime from '../../lib/runtime/runtime';

import { getUserProfile,login } from "../../utils/asyncWx";

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  handleGetUserInfo(e){
    // console.log(e);
    const {userInfo} = e.detail;
    wx.setStorageSync('userinfo', userInfo);
    // 跳转回上一级页面
    wx.navigateBack({
      delta: 1
    });
  },
  async getUserProfile(e){
    /* 获取用户的个人信息 */
    const res = await  getUserProfile();
    
     /* 获取用户的token值 */
     const { encryptedData, rawData, iv, signature } = res;
     // 2 获取小程序登录成功后的code
     const { code } = await login();
     const loginParams = { encryptedData, rawData, iv, signature, code };
     // 3 发送请求 获取用户的token，res为null，因为没有接口的权限
     // token是用户的唯一凭据，后期用在其他敏感接口的验证
     const res1 = await request({ url: "/users/wxlogin", data: loginParams, method: "POST" });
     console.log("res1: "+res1); /* 输出结果：res1: null */
     /* 因为只有企业小程序才有支付权限 */
     let token = "";
     if (res1 === null) {
       token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo";
     }
     // 4 把token存入缓存中
     wx.setStorageSync("token", token);

     const {userInfo}=res;
     wx.setStorageSync('userinfo', userInfo);
      // 跳转回上一级页面
     wx.navigateBack({
       delta: 1
     });
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})